/**
 * 
 */
package br.com.locasis.view.validator;

import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

/**
 * @author rlresende
 * 
 */
@org.jboss.seam.annotations.faces.Validator
@Name("placaValidator")
@BypassInterceptors
public class PlacaValidator implements Validator {

	@Override
	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		if (value != null && value.toString().replace("-", "").length() > 7) {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "A quantidade de digitos não pode exceder 7 caracteres."));
		}
		Pattern p = Pattern.compile("[a-zA-Z]{3}[0-9]{4}");
		if (!p.matcher(value.toString()).matches()) {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "A placa informada é inválida."));
		}
	}

}
